package com.hup.joystick.service;

import com.hup.joystick.constant.FunctionConstant.QuickMenuFunctionType;
import com.hup.joystick.model.QuickMenuFunction;
import com.hup.joystick.view.volumeMenu.VolumeMenu;
import com.hup.utils.commons.TextUtil;
import com.hup.utils.commons.hardware.HRobotJni;
import com.hup.utils.commons.interfaces.Runners.RunAndThrowable;

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.function.Predicate;

/**
 * @author hugan
 * @date 2020/11/10
 */
public class QuickMenuFunctions {
    /**
     * 内置键盘功能的[keyText,desc]map
     */
    private static final HashMap<String, String> MAP_KEYBOARD_DESC = new HashMap<>();

    /**
     * 内置的命令
     */
    private static final LinkedHashMap<String, QuickMenuFunction> BUILD_IN_FUNCTION = new LinkedHashMap<>();

    /*
     * 添加内置功能
     */
    static {
        cmd("音量", VolumeMenu::showFrame);
        cmd("锁屏", HRobotJni::lockWorkStation);
        cmd("播放/暂停", HRobotJni::mediaPlayPause);
        keyboard("桌面", new Integer[]{KeyEvent.VK_WINDOWS, KeyEvent.VK_D});
        keyboard("我的电脑", new Integer[]{KeyEvent.VK_WINDOWS, KeyEvent.VK_E});
        keyboard("任务管理器", new Integer[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT, KeyEvent.VK_ESCAPE});
        keyboard(new Integer[]{KeyEvent.VK_WINDOWS});
        keyboard(new Integer[]{KeyEvent.VK_ALT, KeyEvent.VK_F4});
    }

    private static void cmd(String desc, RunAndThrowable action) {
        QuickMenuFunction function = new QuickMenuFunction();
        function.type = QuickMenuFunctionType.CMD;
        function.action = action;
        function.cmdDesc = desc;
        BUILD_IN_FUNCTION.put(desc, function);
    }

    private static void keyboard(Integer[] keyCodes) {
        QuickMenuFunction function = new QuickMenuFunction();
        function.type = QuickMenuFunctionType.KEYBOARD;
        function.lstKeyCode = Arrays.asList(keyCodes);
        BUILD_IN_FUNCTION.put(function.toString(), function);
    }

    private static void keyboard(String desc, Integer[] keyCodes) {
        QuickMenuFunction function = new QuickMenuFunction();
        function.type = QuickMenuFunctionType.KEYBOARD;
        function.lstKeyCode = Arrays.asList(keyCodes);
        String key = TextUtil.getText(function.lstKeyCode, "+", null, null,
                (sb, hasNext, next) -> sb.append(KeyEvent.getKeyText(next)));
        MAP_KEYBOARD_DESC.put(key, desc);
        BUILD_IN_FUNCTION.put(function.toString(), function);
    }

    public static String getKeyBoardDesc(String descOfKey) {
        String buildInDesc = MAP_KEYBOARD_DESC.get(descOfKey);
        return buildInDesc == null ? descOfKey : buildInDesc;
    }

    /**
     * 获取内置功能
     */
    public static ArrayList<QuickMenuFunction> getBuildInFunctions() {
        ArrayList<QuickMenuFunction> res = new ArrayList<>(BUILD_IN_FUNCTION.size());
        res.addAll(BUILD_IN_FUNCTION.values());
        return res;
    }

    /**
     * 获取内置功能
     */
    public static ArrayList<QuickMenuFunction> getBuildInFunctions(Predicate<QuickMenuFunctionType> predicate) {
        ArrayList<QuickMenuFunction> res = new ArrayList<>(BUILD_IN_FUNCTION.size());
        for (QuickMenuFunction value : BUILD_IN_FUNCTION.values()) {
            if (predicate.test(value.type)) res.add(value);
        }
        return res;
    }

    /**
     * 获取内置[命令功能]的action
     *
     * @return nullable
     */
    public static RunAndThrowable getCmdAction(QuickMenuFunction function) {
        QuickMenuFunction value = BUILD_IN_FUNCTION.get(function.cmdDesc);
        if (value != null) return value.action;
        return null;
    }

}
